<template>
  <!-- 主体区域 -->
  <section id="app">
    <!-- 输入框 -->
    <todo-header  @add="headerlAdd"></todo-header>
    <!-- 列表区域 -->
    <todo-manin :list="list" @del="headerldel"></todo-manin>
    <!-- 统计和清空 -->
    <todo-footer :list="list" @clear="list = []"></todo-footer>
   
  </section>
</template>

<script>

import todoHeader from './components/todoHeader.vue'
import todoManin from './components/todoManin.vue'
import todoFooter  from './components/todoFooter.vue'


export default {
  components: {
    todoHeader,
    todoManin,
    todoFooter
  },
  
  data () {
    return {
      list: JSON.parse(localStorage.getItem('todolist')) || [
        {id:1, name: '吃饭'},
        {id:2, name: '自习'},
        {id:3, name: '睡觉'}
      ]
    }
  },
    methods: {
    headerldel(id) {
      // console.log(id)
      this.list = this.list.filter(item => item.id !== id)
    },

    headerlAdd(toduname) {
      this.list.unshift(
        {
          id: Date.now(),
          name: toduname
        }
      )
    },
  },
  watch: {
    list: {
      handler(val) {
        localStorage.setItem('todolist', JSON.stringify(val))
      },
      deep: true
    }
  },

}
</script>

<style>

</style>
